<template>
  <div class="login">
    <el-form ref="form" :model="form" class="container" :rules="rules">
      <el-form-item>
        <div class="avatar">
          <img src="../../assets/logo.png" alt="" />
        </div>
      </el-form-item>
      <el-form-item prop="username">
        <el-input
          v-model="form.username"
          placeholder="账户"
          prefix-icon="myicon myicon-user"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="form.password"
          placeholder="密码"
          prefix-icon="myicon myicon-key"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="submitForm('form')"
          >登录</el-button
        >

        <el-button @click="resetForm('form')" class="reset-btn">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {GetLoginUrl} from "../../api/index.js"
export default {
  data() {
    
    var validatePwd = (rule, value, callback) => {
      
      if (!value) return callback(new Error("密码不能为空"));

      if (!value.includes("1")) {
        callback(new Error("密码必须包含1"));
      } else {
        callback();
      }
    };

    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],

        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
          // 自定义密码规则
          { validator: validatePwd, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
  async submitForm(formName) {
      GetLoginUrl(this.form).then(res=>console.log(res.data))
      try{

        let success = await this.$refs[formName].validate();

        if(!success) return false;
        // 解构赋值
        let {
            data:{username,token},
            meta:{status}
        } = await GetLoginUrl(this.form)
        
        if(status == 200){
             this.$message.success("恭喜你登录成功！");
             this.$store.commit("setToken",token);
            console.log(token);
             this.$store.commit("setUsername",username);
             this.$router.push({path:'/home'});
            //  this.$message({
            //     message: '恭喜你，登录成功！',
            //     type: 'success'
            //     });
        }
      }catch(error){
        console.log(error)
      }
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       alert("submit!");
    //     //   获取数据
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped lang="less">
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn,
    .reset-btn {
      width: 100%;
      margin-top: 5px;
    }

    .el-button {
      margin-left: 0;
    }
  }
}
</style>